﻿using System;
using System.Collections.Generic;
using DataAccessLibrary.DAL;
using entities;

namespace BusinessLogicLibrary
{
    public class PromotionModule{

        private readonly PromotionDAO _promotionDAO = new PromotionDAO();
        private const string Error = "ERROR: ";

        public List<vSubPromotion> GetPromotion (bool available)
        {
            return available ? _promotionDAO.SelectPromotion(1) : _promotionDAO.SelectPromotion(-1);
        }

        public Promotion GetPromotion (int id)
        {
            return _promotionDAO.SelectPromotionById(id);
        }

        public List<vSubPromotion> GetPromotion (DateTime begin, DateTime end)
        {
            return _promotionDAO.SelectPromotionByTime(begin, end);
        }

        public List<vSubPromotion> GetPromotion(string title)
        {
            return _promotionDAO.SelectPromotionByTitle(title);
        }

        public string AddPromotion(DateTime startTime, DateTime endTime, string title, string desciption, byte[] banner)
        {
          
          
            try
            {
                if (title.Trim()!="")
                {
                    if (desciption.Trim()!="")
                    {
                        var promotion = new Promotion
                                            {
                                                PromotionEnd = endTime,
                                                PromotionBanner = banner,
                                                PromotionTitle = title,
                                                PromotionDescription = desciption,
                                                PromotionStart = startTime
                                                
                                            };
                        return _promotionDAO.InsertPromotion(promotion);
                    } return Error + "Desciption is empty";
                }
                return Error + "Promotion Title is empty";
            }
            catch (Exception)
            {
                return Error;
            }
        }

        public string ModifyPromotion(int promotionId,DateTime startTime, DateTime endTime, string title, string desciption, byte[] banner)
        {
            try
            {
                if (title.Trim() != "")
                {
                    if (desciption.Trim() != "")
                    {
                        var promotion = new Promotion
                        {
                            PromotionEnd = endTime,
                            PromotionStart = startTime,
                            PromotionBanner = banner,
                            PromotionTitle = title,
                            PromotionDescription = desciption,
                            PromotionID =  promotionId
                        };
                        return _promotionDAO.UpdatePromotion(promotion);
                    } return Error + "Desciption is empty";
                }
                return Error + "Promotion Title is empty";
            }
            catch (Exception)
            {
                return Error;
            }
        }

        public string removePromotion(int promotionId)
        {
                return _promotionDAO.DeletePromotion(promotionId);
        }

        public List<vSubPromotionDetail> GetPromotionDetailByPromotionId(int promotionId)
        {
            return _promotionDAO.SelectPromotionDetailByPromotionId(promotionId);
        }
         public PromotionDetail GetPromotionDetail(int promotionDetailId)
         {
             return _promotionDAO.SelectPromotionDetail(promotionDetailId);
         }

         public string AddPromotionDetail(int promotionId, string promotionContent, byte[] promotionImage)
         {
             try
             {
                 if (promotionContent.Trim() != "")
                 {
                     return _promotionDAO.InsertPromotionDetail(promotionId, promotionContent, promotionImage);
                 }
                 return Error + "Promotion Content is empty";
             }
             catch (Exception ex)
             {
                 return Error + ex.Message;
             }

         }

        public string ModifyPromotionDetail(int promotionDetailId,string promotionContent, byte[] promotionImage)
        {
            try
            {
                if (promotionContent.Trim() != "")
                {
                    return _promotionDAO.UpdatePromotionDetail(promotionDetailId,promotionContent, promotionImage);
                }
                return Error + "Promotion Content is empty";
            }
            catch (Exception ex)
            {
                return Error + ex.Message;
            }
        }

        public string RemovePromotionDetail(int promotionDetailId)
        {
            return _promotionDAO.DeletePromotionDetail(promotionDetailId);
        }
    }
}
